/*
 * Copyright (c) 2012, marco.tamburelli@gmail.com
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met: 
 * 
 * 1. Redistributions of source code must retain the above copyright notice, this
 *    list of conditions and the following disclaimer. 
 * 2. Redistributions in binary form must reproduce the above copyright notice,
 *    this list of conditions and the following disclaimer in the documentation
 *    and/or other materials provided with the distribution. 
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
package org.bmi.gwt.mi.server.events;

import javax.servlet.ServletContext;

import org.bmi.gwt.mi.shared.BroadChannel;
import org.bmi.gwt.mi.shared.Channel;
import org.bmi.gwt.mi.shared.exceptions.NameParseException;

/**
 * This is a queue event. Such events are normally fired when a queue is just
 * created or just destroyed.
 * 
 * @author marco.tamburelli@gmail.com
 */
public interface QueueEvent
{
	/**
	 * This method returns a channel to the event source queue. The source queue
	 * can be: <br>
	 * <br>
	 * 
	 * 1. the queue triggering the event through a defined
	 * {@link QueueEventHandler}.<br>
	 * <br>
	 * 
	 * 2. the target queue of a method invocation request, when a client is
	 * polling.<br>
	 * <br>
	 * 
	 * In both cases the queue can't be bound by the server, it's just an
	 * element that can be used to check attributes and create proxyes.
	 * 
	 * @return The requested queue.
	 * @throws NameParseException
	 */
	Channel getSourceQueueChannel();

	/**
	 * The method returns a broad channel.
	 * 
	 * @param queueExpression The expression of the queue group. Note that the
	 *        expression must identify a pool: must be of the form
	 *        <code>org.test.*</code>
	 * @return The requested queue pool.
	 * @throws NameParseException In case the queue expression is not a valid
	 *         one.
	 */
	BroadChannel getBroadChannel(String queueExpression) throws NameParseException;

	/**
	 * The method returns a channel to a queue. If the queue has been bound by
	 * some client, can be used only to check attributes and and create proxyes.
	 * 
	 * @param queueName The name of the queue.
	 * @return The requested queue
	 * @throws NameParseException In case the queue expression is not a valid
	 *         one.
	 */
	Channel getChannel(String queueName) throws NameParseException;

	/**
	 * This method returns <code>true</code> if the query with provided name has
	 * been created and it's active.
	 * 
	 * @param queueName The name of a query.
	 * @return <code>true</code> or <code>false</code>.
	 * @throws NameParseException In case the queue name is not valid.
	 */
	boolean isQueueAlive(String queueName) throws NameParseException;

	/**
	 * Returns the value of an attributes registered on target queue session.
	 * 
	 * The queue session is an useful replacement of servlet session, since the
	 * last, in handlers context, is not reachable in an easy way.
	 * 
	 * @param name The name which identify the attributes
	 * @return
	 */
	<T> T getQueueSessionAttribute(String name);

	/**
	 * Registers an attribute on the target queue session.
	 * 
	 * The queue session is an useful replacement of servlet session, since the
	 * last, in handlers context, is not reachable in an easy way.
	 * 
	 * @param name The name which identifies the attribute.
	 * @param value the value of the attribute.
	 */
	<T> void setQueueSessionAttribute(String name, T value);

	/**
	 * Removes from the target queue session an attribute, and returns its
	 * value.
	 * 
	 * The queue session is an useful replacement of servlet session, since the
	 * last, in handlers context, is not reachable in an easy way.
	 * 
	 * @param name the name which identifies the attribute.
	 * @return
	 */
	<T> T removeQueueSessionAttribute(String name);

	/**
	 * Useful function providing access to the ervlet context.
	 * 
	 * Since the BMI tool doesn't provide easy access to the servlet container
	 * features, this method ensure that at least the global servlet session can
	 * be reached within a queue event. <br>
	 * <br>
	 * THIS FEATURE IS HOWEVER EXPERIEMNTAL
	 * 
	 * @return
	 */
	ServletContext getServletContext();
}
